package com.example.spring.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.spring.demo.common.error.CustomException;
import com.example.spring.demo.common.model.req.ReportParam;
import com.example.spring.demo.common.model.vo.ReportVo;
import com.example.spring.demo.entity.GoodsReport;
import com.example.spring.demo.service.GoodsReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/12/31 10:55
 **/
@RequestMapping("/api/report")
@RestController
public class GoodsReportController {
    @Autowired
    private GoodsReportService goodsReportService;
    //举报
    @PostMapping("/report")
    public String report(@RequestBody GoodsReport report){
        //判断是否已经举报
        QueryWrapper<GoodsReport> query = new QueryWrapper<>();
        query.lambda().eq(GoodsReport::getGoodsId,report.getGoodsId())
                .eq(GoodsReport::getReportUser,report.getReportUser());
        GoodsReport one = goodsReportService.getOne(query);
        if(one != null){
            throw new CustomException("您已经举报，不用重复举报!");

        }
        report.setReportTime(new Date());
        if(goodsReportService.save(report)){
            return "举报成功!";
        }
        throw new CustomException("举报失败!");
    }

    //查询是否已经举报
    @GetMapping("/hasReport")
    public String hasReport(Long userId, Long goodsId){
        QueryWrapper<GoodsReport> query = new QueryWrapper<>();
        query.lambda().eq(GoodsReport::getGoodsId,goodsId)
                .eq(GoodsReport::getReportUser,userId);
        GoodsReport one = goodsReportService.getOne(query);
        if(one != null){ //已经举报
            return "1";
        }else{
            return "0";
        }
    }

    //查询列表
    @GetMapping("/getList")
    public IPage<ReportVo> getList(ReportParam param){
        return goodsReportService.getList(param);
    }
    //处理举报内容
    @PostMapping("/doReport")
    public String doReport(@RequestBody GoodsReport report) {
        UpdateWrapper<GoodsReport> update = new UpdateWrapper<>();
        update.lambda().set(GoodsReport::getStatus, "1") //状态为 1 标识已处理
                .eq(GoodsReport::getReportId, report.getReportId());
        if (goodsReportService.update(update)) {
            return "处理成功!";
        }
        throw new CustomException("处理失败!");
    }
}
